package com.example.pinkcat.fragment.shouye;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.pinkcat.R;
import com.example.pinkcat.adapter.Remen_FragmentPagerAdapter;
import com.example.pinkcat.api.getData.getData_waibu;

public class Fragment_remen extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

    Context context;
    private Button button1;
    private Button button2;

    public RecyclerView recyclerView;
    public RecyclerView.LayoutManager mLayoutManager;
    public Remen_FragmentPagerAdapter remen_Adapter;
    private SwipeRefreshLayout swipeRefreshLayout;

    public String style;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_remen,container,false);
    }

    @Override
    public void onResume() {
        super.onResume();
        getData_waibu.getdayhot(getContext(),remen_Adapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = view.getContext();
        view.findViewById(R.id.dayhot).setSelected(true);
        view.findViewById(R.id.weekhot).setSelected(false);

        initRecyclerView(view);

    }

    private void initRecyclerView(View view) {
        //
        recyclerView = view.findViewById(R.id.remen_RV);
        //设置布局
        mLayoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);

        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.remen_srl);//初始
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.pink,R.color.purple_200);

        remen_Adapter = new Remen_FragmentPagerAdapter(view.getContext(),style);
        recyclerView.setAdapter(remen_Adapter);

            //日排行按钮监听
        view.findViewById(R.id.dayhot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //设置数据
                getData_waibu.getdayhot(view.getContext(),remen_Adapter);
                view.findViewById(R.id.dayhot).setSelected(true);
                view.findViewById(R.id.weekhot).setSelected(false);
            }
        });

           // 周排行按钮监听
        view.findViewById(R.id.weekhot).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //设置数据
                getData_waibu.getweeekhot(view.getContext(),remen_Adapter);
                view.findViewById(R.id.dayhot).setSelected(false);
                view.findViewById(R.id.weekhot).setSelected(true);
            }
        });


    }

    //下拉刷新
    public void onRefresh(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("fragment","下拉刷新");
                int pageNumer = 0;;
                PostData(); //加载数据
                swipeRefreshLayout.setRefreshing(false);
            }
        },1000);
    }


    public void PostData(){
        getData_waibu.getdayhot(context,remen_Adapter);
    }
}
